package com.ciquan.server.controller;

import com.ciquan.server.bean.BusinessStatus;
import com.ciquan.server.service.BusinessStatusService;
import com.ciquan.server.util.ExcelUtils;
import com.ciquan.server.util.web.MenuUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by kakalee on 16/3/5.
 */
@Controller
@RequestMapping("/businessExcel")
public class BusinessExcelController extends AbstractController {

    @Resource
    private BusinessStatusService businessStatusService;

    @RequestMapping("index")
    public ModelAndView index() {
        return new ModelAndView("businessExcel");
    }

    @RequestMapping(value = "upload", method = {RequestMethod.POST})
    public ModelAndView upload(@RequestParam(value = "file") MultipartFile file,
                       @RequestParam(value = "type") Integer type) throws Exception{
        List<BusinessStatus> businessStatuses = ExcelUtils.parse(file.getInputStream(), BusinessStatus.class);
        for(BusinessStatus businessStatus : businessStatuses) {
            businessStatus.setType(type);
            businessStatusService.insert(businessStatus);
        }
        return new ModelAndView("redirect:/" + MenuUtils.ADMIN_MENUS.get(0).getPath());
    }

}
